import Mock from 'mockjs'
import svgCaptcha from 'svg-captcha'

// 自定义扩展 - 生成无连接符的GUID
Mock.Random.extend({
    guidWithoutDash: function () {
        return this.guid().replace(/-/g, '')
    }
})

// 用户数据
const users = {
    admin: {
        id: '0aa01bb4d3f7423f9e11336cf5ca6ef5',
        deptId: '88921a7b70a04ea2b7d1db1651ffe666',
        name: 'admin',
        email: '1593xxx7442@qq.com',
        phoneNumber: '17625XXX1384',
        sex: '1',
        password: '',
        avatar: null,
        status: 1,
        loginIp: Mock.mock('@ip'),
        loginTime: new Date(),
        createBy: 'admin',
        createTime: '2024-08-24 21:43:28',
        updateBy: null,
        updateTime: null,
        remark: null
    },
    user: {
        id: '9057fae81b7345e2ac725346278fb9bb',
        deptId: '88921a7b70a04ea2b7d1db1651ffe666',
        name: '测试用户',
        email: '12345678@qq.com',
        phoneNumber: '12345678901',
        sex: '1',
        password: '',
        avatar: null,
        status: 1,
        loginIp: Mock.mock('@ip'),
        loginTime: new Date(),
        createBy: 'admin',
        createTime: '2024-03-09 13:27:33',
        updateBy: null,
        updateTime: null,
        remark: null
    }
}

// 角色数据
const roles = {
    admin: [
        {
            id: '36f0df335b6a4293b3ebd8e7a27e6026',
            name: '超级管理员',
            roleKey: 'admin',
            seqNo: 0,
            status: 0,
            createBy: 'admin',
            createTime: '2024-03-07 20:13:55',
            updateBy: null,
            updateTime: null,
            remark: null
        }
    ],
    common: [
        {
            id: '3f536e29949b4723a8fb4514411d50df',
            name: '一般人员',
            roleKey: 'common',
            seqNo: 0,
            status: 0,
            createBy: 'common',
            createTime: '2024-03-07 20:13:33',
            updateBy: null,
            updateTime: null,
            remark: null
        }
    ]
}

// 权限数据
const permissions = {
    admin: [
        {
            id: 'd815a5167ad84c2aab29aa9ab080ca7c',
            name: '系统管理',
            parentId: null,
            seqNo: 0,
            menuType: 'D',
            url: '/system',
            perms: 'system',
            status: 1,
            hidden: 0,
            alwaysShow: 0,
            redirect: '/system/user',
            component: 'layout',
            href: '',
            icon: 'ep:setting',
            noCache: 0,
            affix: 0,
            breadcrumb: 1,
            activeMenu: '',
            createBy: 'admin',
            createTime: '2024-03-07 20:15:00',
            updateBy: null,
            updateTime: null,
            remark: null
        },
        {
            id: '0fdf3ff4c5b348deb960e989e6ee83af',
            name: '用户管理',
            parentId: 'd815a5167ad84c2aab29aa9ab080ca7c',
            seqNo: 1,
            menuType: 'M',
            url: 'user',
            perms: 'system:user:list',
            status: 1,
            hidden: 0,
            alwaysShow: 0,
            redirect: null,
            component: 'views/system/user',
            href: '',
            icon: 'mdi:user',
            noCache: 0,
            affix: 0,
            breadcrumb: 1,
            activeMenu: '',
            createBy: 'admin',
            createTime: '2024-03-09 13:15:19',
            updateBy: null,
            updateTime: null,
            remark: null
        },
        {
            id: 'abb861bd5da94a4e884b5e70758754be',
            name: '角色管理',
            parentId: 'd815a5167ad84c2aab29aa9ab080ca7c',
            seqNo: 2,
            menuType: 'M',
            url: 'role',
            perms: 'system:role:list',
            status: 1,
            hidden: 0,
            alwaysShow: 0,
            redirect: null,
            component: 'views/system/role',
            href: '',
            icon: 'icon-park-outline:peoples',
            noCache: 0,
            affix: 0,
            breadcrumb: 1,
            activeMenu: '',
            createBy: 'admin',
            createTime: '2024-03-09 13:18:03',
            updateBy: null,
            updateTime: null,
            remark: null
        },
        {
            id: 'c1ab532b91fb405fb223ef679a656c8f',
            name: '菜单管理',
            parentId: 'd815a5167ad84c2aab29aa9ab080ca7c',
            seqNo: 3,
            menuType: 'M',
            url: 'menu',
            perms: 'system:menu:list',
            status: 1,
            hidden: 0,
            alwaysShow: 0,
            redirect: null,
            component: 'views/system/menu',
            href: '',
            icon: 'ic:round-menu',
            noCache: 0,
            affix: 0,
            breadcrumb: 1,
            activeMenu: '',
            createBy: 'admin',
            createTime: '2024-03-09 13:19:09',
            updateBy: null,
            updateTime: null,
            remark: null
        },
        {
            id: '233daa1502c344d685adc9341d11d1e9',
            name: '部门管理',
            parentId: 'd815a5167ad84c2aab29aa9ab080ca7c',
            seqNo: 4,
            menuType: 'M',
            url: 'dept',
            perms: 'system:dept:list',
            status: 1,
            hidden: 0,
            alwaysShow: 0,
            redirect: null,
            component: 'views/system/dept',
            href: '',
            icon: 'clarity:tree-view-line',
            noCache: 0,
            affix: 0,
            breadcrumb: 1,
            activeMenu: '',
            createBy: 'admin',
            createTime: '2024-03-09 13:19:38',
            updateBy: null,
            updateTime: null,
            remark: null
        }
    ],
    common: [
        {
            id: 'M101',
            name: '个人中心',
            parentId: null,
            seqNo: 1,
            menuType: 'M',
            url: 'profile',
            perms: 'profile:view',
            status: 1,
            hidden: 0,
            alwaysShow: 0,
            redirect: null,
            component: 'views/profile/index',
            href: '',
            icon: 'user',
            noCache: 1,
            affix: 0,
            breadcrumb: 0,
            activeMenu: '',
            createBy: 'system',
            createTime: '2024-03-07 20:15:00',
            updateBy: null,
            updateTime: null,
            remark: '个人中心菜单'
        }
    ]
}

function generateCaptchaImage() {
    const captcha = svgCaptcha.create({
        width: 190, // 宽度
        height: 50, // 高度,
        fontSize: 50, // 字体大小
        size: 5, // 验证码长度
        ignoreChars: '0o1i', // 排除易混淆字符
        noise: 2, // 干扰线数量
        color: true, // 彩色字符
        background: '#C0C0C0' // 背景色
    })
    return {
        text: captcha.text, // 验证码文本
        data: captcha.data // SVG 数据（可直接返回给前端）
    }
}

export default [
    // 获取验证码
    {
        url: '/dev-api/auth/captchaImage',
        method: 'get',
        response: () => {
            const captcha = generateCaptchaImage()
            return {
                code: 1,
                msg: '操作成功',
                data: {
                    captchaEnabled: true,
                    uuid: Mock.mock('@guidWithoutDash'),
                    img: `data:image/svg+xml;base64,${Buffer.from(captcha.data).toString('base64')}`,
                    captcha: captcha.text
                }
            }
        }
    },

    // 登录接口
    {
        url: '/dev-api/auth/login',
        method: 'post',
        timeout: 500,
        response: ({ body }) => {
            const { username, password, captcha, uuid } = body

            // 验证码校验（模拟）
            if (!captcha || !uuid) {
                return {
                    code: 500,
                    msg: '验证码不能为空'
                }
            }

            // 用户验证
            const validUsers = [
                {
                    username: 'admin',
                    password: 'admin123',
                    role: 'admin'
                },
                {
                    username: 'user',
                    password: 'user123',
                    role: 'common'
                }
            ]

            const user = validUsers.find((u) => u.username === username)

            if (!user || user.password !== password) {
                return {
                    code: 500,
                    msg: '用户不存在/密码错误'
                }
            }

            return Mock.mock({
                code: 1,
                msg: '登录成功',
                data: {
                    token: '@guidWithoutDash'
                }
            })
        }
    },

    // 获取用户信息
    {
        url: '/dev-api/system/block/user/getInfo',
        method: 'get',
        response: (req) => {
            // 从请求头获取token
            const token = req.headers.authorization

            if (!token) {
                return {
                    code: 401,
                    msg: '未授权访问'
                }
            }

            // 根据实际情况确定用户角色，这里简化为总是返回admin
            // 实际应用中应该根据token解析用户角色
            const userRole = 'admin'

            return Mock.mock({
                code: 1,
                msg: '操作成功',
                data: {
                    user: users[userRole] || users.user,
                    roles: roles[userRole] || roles.common,
                    permissions: permissions[userRole] || permissions.common
                }
            })
        }
    },

    // 退出登录
    {
        url: '/dev-api/auth/logout',
        method: 'post',
        response: () => {
            return {
                code: 1,
                msg: '退出成功'
            }
        }
    }
]
